package com.neusoft.utils.springmvc;

import java.text.ParseException;
import java.util.Date;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.core.convert.converter.Converter;

import com.neusoft.utils.convert.ConvertUtils;
import com.neusoft.utils.date.DateUtil;
import com.neusoft.utils.string.StringUtil;

public class MyDateConverter implements   Converter<String, Date> {
	private static final String[] pattern = new String[] { "yyyy-MM-dd HH","yyyy-MM-dd", "yyyy-MM", "yyyy-MM-dd HH:mm",
		"yyyy-MM-dd HH:mm:ss","yyyy-MM-dd HH:mm:ss.s", "yyyy/MM", "yyyy/MM/dd", "yyyy/MM/dd HH", "yyyy/MM/dd HH:mm", "yyyy/MM/dd HH:mm:ss" };

	@Override
	public Date convert(String source) {
		if(null==source||StringUtil.isBlank(source)||"null".equalsIgnoreCase(source)){
			return null;
		}
		if(NumberUtils.isNumber(source)){
			return 	new Date(ConvertUtils.toLongValue(source));
		}
		try {
			return DateUtil.parseDate(source.trim(), pattern);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	public static void main(String[] args) throws ParseException {
		System.err.println( DateUtil.parseDate("", pattern));
	}

}